package com.example.orchid.coordinatorlayoutdemo.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.orchid.coordinatorlayoutdemo.R;
import com.example.orchid.coordinatorlayoutdemo.entity.NewsListEntity.StoriesEntity;
import com.example.orchid.coordinatorlayoutdemo.utils.DateUtils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;

/**
 * Created by Orchid on 2015-10-22.
 */
public class ZhihuRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<StoriesEntity> mStories;
    private OnItemClickListener listener;

    // 带图item
    private static final int TYPE_0 = 0;

    // 不带图item
    private static final int TYPE_1 = 1;

    // tag
    private static final int TYPE_2 = 2;

    public ZhihuRecyclerAdapter(Context context, ArrayList<StoriesEntity> stories) {
        this.context = context;
        this.mStories = stories == null ? new ArrayList<>() : new ArrayList<>(stories);;
    }

    /**
     * 决定元素的布局使用哪种类型
     *
     * @param position 数据源List的下标
     * @return 一个int型标志，传递给onCreateViewHolder的第二个参数
     */
    @Override
    public int getItemViewType(int position) {
        StoriesEntity storiesEntity = mStories.get(position);

        if (storiesEntity.isTag()) {
            return TYPE_2;
        } else {
            return TYPE_0;
        }

    }

    /**
     * 渲染具体的ViewHolder
     *
     * @param parent   ViewHolder的容器
     * @param viewType 一个标志，我们根据该标志可以实现渲染不同类型的ViewHolder
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_0) {
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_base_swipe_list, parent, false);
            return new NormalItemHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_base_swipe_group_item, parent, false);
            return new GroupItemHolder(view);
        }

    }

    @Override
    /**
     * 绑定ViewHolder的数据。
     *
     * @param holder
     * @param position 数据源list的下标
     */
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final StoriesEntity bean = mStories.get(position);

        if (null == bean)
            return;

        if (holder instanceof GroupItemHolder) {
            GroupItemHolder groupItemHolder = (GroupItemHolder) holder;
            String dateStr = DateUtils.getFormatTime(bean.getTitle(), DateUtils.YYYYMMDD, DateUtils.MMDD);
            String week = DateUtils.getWeekOfDate(DateUtils.str2Date(bean.getTitle(), DateUtils.YYYYMMDD));
            groupItemHolder.newsTime.setText(dateStr + " " + week);
        } else {
            NormalItemHolder normalItemHolder = (NormalItemHolder) holder;
            normalItemHolder.title.setText(bean.getTitle());
            Uri uri = Uri.parse(bean.getImages().get(0));
            normalItemHolder.imageView.setImageURI(uri);
            /**
             * 调用接口回调
             */
            holder.itemView.setOnClickListener(v ->  {
                if (null != listener)
                    listener.onItemClick(position, bean);
            });
        }




    }


    @Override
    public int getItemCount() {
        return null == mStories ? 0 : mStories.size();
    }

    public class NormalItemHolder extends RecyclerView.ViewHolder {

        SimpleDraweeView imageView;
        TextView title;

        public NormalItemHolder(View view) {
            super(view);
            imageView = (SimpleDraweeView) view.findViewById(R.id.avatar);
            title = (TextView) view.findViewById(R.id.title);
        }
    }

    /**
     * 带日期新闻标题
     */
    public class GroupItemHolder extends RecyclerView.ViewHolder {
        TextView newsTime;

        public GroupItemHolder(View itemView) {
            super(itemView);
            newsTime = (TextView) itemView.findViewById(R.id.date_text);
        }
    }

    public void updateData(ArrayList<StoriesEntity> list) {
        this.mStories = list == null ? new ArrayList<>() : new ArrayList<>(list);
        this.notifyDataSetChanged();
    }

    /**
     * 内部接口回调方法
     */
    public interface OnItemClickListener {
        void onItemClick(int position, Object object);
    }

    /**
     * 设置监听方法
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
